#include "vt_recode_mp3.h"
                     
enum vt_recode_mp3_statue recode_state = VT_RECODE_STOP_STATUE;
enum vt_recode_mp3_statue mp3_state = VT_MP3_STOP_STATUE;

enum vt_mp3_action_statue mp3_action_statue = MP3_ACTION_NONE;

uint8_t vt_dsp_mp3 = 0;  //0 : CLOSE  1: OPEN

void vt_cmd_recv_handler(uint8_t *data, uint16_t length)
{
	for(uint8_t i = 0; i < length; i++)
		printf("%c",data[i]);
	
	bool ret;
	printf("vt_dsp_mp3 = %d\r\n",vt_dsp_mp3);
	switch(data[0])
	{
		
		case 'A':
			if(recode_state == VT_RECODE_STOP_STATUE)
			{
				printf("task case UERR_EVT_RECODE_START\r\n");
				dsp_close();
				ret = vt_recode_mp3(UERR_EVT_RECODE_START, NULL);
			}
			
			if(ret != true)
			{
				printf("VT# %c  faild\r\n",data[0]);
			}
			break;
			
		case 'B':
			if(recode_state == VT_RECODE_START_STATUE)
			{
				printf("task case UERR_EVT_RECODE_STOP\r\n");
				ret = vt_recode_mp3(UERR_EVT_RECODE_STOP, NULL);
			}
			
			if(ret != true)
			{
				printf("VT# %c  faild\r\n",data[0]);
			}
			break;
			
		case 'C':
			//if(mp3_state == VT_MP3_STOP_STATUE)
			{
				printf("task case UERR_EVT_MP3_START\r\n");
				printf("vt_dsp_mp3 = %d\r\n",vt_dsp_mp3);
				dsp_close();
				ret = vt_recode_mp3(UERR_EVT_MP3_START, NULL);
			}
			
			if(ret != true)
			{
				printf("VT# %c  faild\r\n",data[0]);
			}
			break;
		case 'D':
			//if(mp3_state == VT_MP3_START_STATUE)
			{
				printf("task case UERR_EVT_MP3_STOP\r\n");
				vt_dsp_mp3 = 0;
				printf("'D' vt_dsp_mp3 = %d\r\n",vt_dsp_mp3);
				ret = vt_recode_mp3(UERR_EVT_MP3_STOP, NULL);
			}
			
			if(ret != true)
			{
				printf("VT# %c  faild\r\n",data[0]);
			}
			break;
			
	}
}

bool vt_recode_mp3(uint8_t event, void *arg)
{
	bool ret = true;
	switch(event)
	{
		case UERR_EVT_RECODE_START:
			printf("vt_start_recode()\r\n");
			vt_start_recode();
			break;
		
		case UERR_EVT_RECODE_STOP:
			printf("vt_stop_recode()\r\n");
			vt_stop_recode();
			break;
		
		case UERR_EVT_MP3_START:
			//ret = vt_mp3_start();
			vt_dsp_mp3++;
			printf("UERR_EVT_MP3_START vt_dsp_mp3 = %d\r\n",vt_dsp_mp3);
			ret = bt_statemachine(USER_EVT_NATIVE_PLAYBACK_START,NULL);

			break;
		
		case UERR_EVT_MP3_STOP:
			ret = bt_statemachine(USER_EVT_NATIVE_PLAYBACK_STOP,NULL);
			//vt_mp3_stop();
			break;
	}
	return ret;
}


void vt_start_recode()
{
	dsp_open(DSP_LOAD_TYPE_NATIVE_PLAYBACK);
	//if(dsp_open(DSP_LOAD_TYPE_NATIVE_PLAYBACK) == DSP_OPEN_SUCCESS) 
	{
		printf("dsp open RECODE succeed\r\n");
		//dsp_working_label_set(DSP_WORKING_LABEL_VOICE_ALGO);
			
		if(recode_state != VT_RECODE_START_STATUE)
		{
			recode_state = VT_RECODE_START_STATUE;
			dsp_nrec_start_flag = true;
			os_timer_init(&audio_codec_timer,audio_codec_timer_func,NULL);
			os_timer_start(&audio_codec_timer,300,0);
			codec_mic_loop_reg_config();
			ipc_config_mic_only_dma();
			ipc_msg_send((enum ipc_msg_type_t)IPC_MSG_WITHOUT_PAYLOAD, IPC_SUB_MSG_NREC_START, NULL);
			printf("CRETE ****** IPC_SUB_MSG_NREC_START\r\n");
		}
	}

}

void vt_stop_recode()
{
	printf("dsp CLOSE RECODE succeed\r\n");
	if(recode_state != VT_RECODE_STOP_STATUE)
	{
		recode_state = VT_RECODE_STOP_STATUE;
		ipc_msg_send((enum ipc_msg_type_t)IPC_MSG_WITHOUT_PAYLOAD, IPC_SUB_MSG_NREC_STOP, NULL);
		ipc_config_reset_dma();
		printf("CRETE ****** IPC_SUB_MSG_NREC_STOP\r\n");
		//system_sleep_enable();
	}
}

//bool vt_mp3_start()
//{
//	printf("vt_mp3_start\r\n");
//	
//	   printf("mp3_state,%d,%d\r\n",mp3_state,mp3_action_statue);
//    /*
//     * condition:
//     * 1. current state is not PLAY
//     * 2. current action is NONE
//     * 
//     * procedure:
//     * 1. open dsp
//     * 3. send IPC_SUB_MSG_DECODER_START to DSP
//     * 4. receive USER_EVT_DECODER_NEED_DATA from DSP
//     */


//    if((mp3_state == VT_MP3_START_STATUE)
//        || (mp3_action_statue != MP3_ACTION_NONE)) {
//        return false;
//    }
//    if(is_fs_has_mp3_item() == false){
//        return false;
//    }
//    system_sleep_disable();
//    mp3_action_statue = MP3_ACTION_PLAY;
//    
//    sdc_init(0, 0, 7, 0);
//    sdc_bus_set_clk(0);
//    
//    fs_uart_send_mp3_info();

//    if(dsp_open(DSP_LOAD_TYPE_NATIVE_PLAYBACK) == DSP_OPEN_SUCCESS) 
//	{
//       // dsp_working_label_set(DSP_WORKING_LABEL_NATIVE_PLAYBACK);

//        mp3_playback_start();
//    }
//	printf("vt_mp3_start done\r\n");
//    return true;
//}

//void vt_mp3_stop()
//{
//	printf("vt_mp3_stop\r\n");
//}

//void vt_mp3_pause()
//{
//	printf("vt_mp3_pause\r\n");
//}

//static uint32_t dsp_buffer_space = 0;
//static void mp3_playback_start(void)
//{
//    dsp_buffer_space = 0;
//    if(mp3_state != VT_MP3_PAUSE_STATUE){
//        ipc_msg_send((enum ipc_msg_type_t)IPC_MSG_WITHOUT_PAYLOAD, IPC_SUB_MSG_DECODER_START_LOCAL, NULL);
//    }
//    
//    mp3_action_statue = MP3_ACTION_NONE;
//    mp3_state = VT_MP3_START_STATUE;
//    audio_codec_func_start(AUDIO_CODEC_FUNC_NATIVE_PLAYBACK);
//}